Object.defineProperty(exports, "__esModule", { value: !0 }),
  (exports.default = void 0);
var e =
    /^<([-A-Za-z0-9_]+)((?:\s+[a-zA-Z_:][-a-zA-Z0-9_:.]*(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/,
  t = /^<\/([-A-Za-z0-9_]+)[^>]*>/,
  r =
    /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:(?:"((?:\\.|[^"])*)")|(?:'((?:\\.|[^'])*)')|([^>\s]+)))?/g,
  n = l(
    "area,base,basefont,br,col,frame,hr,img,input,link,meta,param,embed,command,keygen,source,track,wbr",
  ),
  a = l(
    "a,address,article,applet,aside,audio,blockquote,button,canvas,center,dd,del,dir,div,dl,dt,fieldset,figcaption,figure,footer,form,frameset,h1,h2,h3,h4,h5,h6,header,hgroup,hr,iframe,isindex,li,map,menu,noframes,noscript,object,ol,output,p,pre,section,script,table,tbody,td,tfoot,th,thead,tr,ul,video",
  ),
  s = l(
    "abbr,acronym,applet,b,basefont,bdo,big,br,button,cite,code,del,dfn,em,font,i,iframe,img,input,ins,kbd,label,map,object,q,s,samp,script,select,small,span,strike,strong,sub,sup,textarea,tt,u,var",
  ),
  i = l("colgroup,dd,dt,li,options,p,td,tfoot,th,thead,tr"),
  c = l(
    "checked,compact,declare,defer,disabled,ismap,multiple,nohref,noresize,noshade,nowrap,readonly,selected",
  ),
  o = l("script,style");
function l(e) {
  for (var t = {}, r = e.split(","), n = 0; n < r.length; n++) t[r[n]] = !0;
  return t;
}
exports.default = function (l) {
  l = (function (e) {
    return e
      .replace(/<\?xml.*\?>\n/, "")
      .replace(/<!doctype.*>\n/, "")
      .replace(/<!DOCTYPE.*>\n/, "");
  })(l);
  var d = [],
    h = { node: "root", children: [] };
  return (
    (function (l, d) {
      var h,
        u,
        f,
        p = [],
        m = l;
      for (
        p.last = function () {
          return this[this.length - 1];
        };
        l;

      ) {
        if (((u = !0), p.last() && o[p.last()]))
          (l = l.replace(
            new RegExp("([\\s\\S]*?)</".concat(p.last(), "[^>]*>")),
            function (e, t) {
              return (
                (t = t.replace(
                  /<!--([\s\S]*?)-->|<!\[CDATA\[([\s\S]*?)]]>/g,
                  "$1$2",
                )),
                d.chars && d.chars(t),
                ""
              );
            },
          )),
            v("", p.last());
        else if (
          (0 === l.indexOf("\x3c!--")
            ? (h = l.indexOf("--\x3e")) >= 0 &&
              (d.comment && d.comment(l.substring(4, h)),
              (l = l.substring(h + 3)),
              (u = !1))
            : 0 === l.indexOf("</")
              ? (f = l.match(t)) &&
                ((l = l.substring(f[0].length)), f[0].replace(t, v), (u = !1))
              : 0 === l.indexOf("<") &&
                (f = l.match(e)) &&
                ((l = l.substring(f[0].length)), f[0].replace(e, g), (u = !1)),
          u)
        ) {
          var b = (h = l.indexOf("<")) < 0 ? l : l.substring(0, h);
          (l = h < 0 ? "" : l.substring(h)), d.chars && d.chars(b);
        }
        if (l === m) throw "Parse Error: ".concat(l);
        m = l;
      }
      function g(e, t, o, l) {
        if (((t = t.toLowerCase()), a[t]))
          for (; p.last() && s[p.last()]; ) v("", p.last());
        if (
          (i[t] && p.last() === t && v("", t),
          (l = n[t] || !!l) || p.push(t),
          d.start)
        ) {
          var h = [];
          o.replace(r, function (e, t) {
            var r = arguments[2]
              ? arguments[2]
              : arguments[3]
                ? arguments[3]
                : arguments[4]
                  ? arguments[4]
                  : c[t]
                    ? t
                    : "";
            h.push({
              name: t,
              value: r,
              escaped: r.replace(/(^|[^\\])"/g, '$1\\"'),
            });
          }),
            d.start && d.start(t, h, l);
        }
      }
      function v(e, t) {
        if (t) for (r = p.length - 1; r >= 0 && p[r] !== t; r--);
        else var r = 0;
        if (r >= 0) {
          for (var n = p.length - 1; n >= r; n--) d.end && d.end(p[n]);
          p.length = r;
        }
      }
      v("", "");
    })(l, {
      start: function (e, t, r) {
        var n = { name: e };
        if (
          (0 !== t.length &&
            (n.attrs = (function (e) {
              return e.reduce(function (e, t) {
                var r = t.value,
                  n = t.name;
                return (
                  e[n] ? (e[n] = "".concat(e[n], " ").concat(r)) : (e[n] = r), e
                );
              }, {});
            })(t)),
          r)
        ) {
          var a = d[0] || h;
          a.children || (a.children = []), a.children.push(n);
        } else d.unshift(n);
      },
      end: function (e) {
        var t = d.shift();
        if (
          (t.name !== e && console.error("invalid state: mismatch end tag"),
          0 === d.length)
        )
          h.children.push(t);
        else {
          var r = d[0];
          r.children || (r.children = []), r.children.push(t);
        }
      },
      chars: function (e) {
        var t = { type: "text", text: e };
        if (0 === d.length) h.children.push(t);
        else {
          var r = d[0];
          r.children || (r.children = []), r.children.push(t);
        }
      },
      comment: function (e) {
        var t = { node: "comment", text: e },
          r = d[0];
        r.children || (r.children = []), r.children.push(t);
      },
    }),
    h.children
  );
};
